/**
 * Baijiahulian.com Inc. Copyright (c) 2014-2016 All Rights Reserved.
 */
package com.baijia.dao.impl;

import com.baijia.dao.AuthorizeServiceDao;
import com.baijia.dao.po.AuthorizeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Date;

/**
 * @title AuthorizeServiceDaoImpl
 * @desc TODO
 * @author macrohuang
 * @date Jan 21, 2016
 * @version 1.0
 */
@Repository
public class AuthorizeServiceDaoImpl implements AuthorizeServiceDao {

    @Autowired
    @Qualifier("jdbcTemplateService")
    private JdbcTemplate jdbcTemplate;

    private static final String SQL =
        "select sid as id,app_id,service_id,name,allow_domains,login_addr,login_success_addr,login_max_times,login_notice_times,ban_seconds from passport_authorized_service where env=?";

    /*
     * (non-Javadoc)
     * 
     * @see com.baijia.dao.AuthorizeServiceDao#getAllAuthorizeServices()
     */
    @Override
    public Collection<AuthorizeService> getAllAuthorizeServices(String env) {
        return jdbcTemplate.query(SQL, new Object[] { env }, new BeanPropertyRowMapper<>(AuthorizeService.class));
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.baijia.dao.AuthorizeServiceDao#getAuthorizeServicesHaveBeenChangeSinceLastTrigger(java.util.Date)
     */
    @Override
    public Collection<AuthorizeService> getAuthorizeServicesHaveBeenChangeSinceLastTrigger(Date date, String env) {
        return jdbcTemplate.query(SQL + "  and updated_at>?", new Object[] { env, date }, new BeanPropertyRowMapper<>(
            AuthorizeService.class));
    }
}
